#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import sys
sys.path.append("..")
import requests
from db.mysql import getConnection

'''
###保存数据###
'''
def insertData(name, photo_url,sku,psku,meterial_main, meterial_sub, meterial_color, origin_price, current_price):
	with getConnection() as db:
		sql = """insert into product(name,photo_url,sku,psku,meterial_main,meterial_sub,meterial_color,origin_price,current_price
						) values ('%s', '%s','%s', '%s','%s', '%s','%s', '%s','%s')"""%(name, photo_url,sku,psku,meterial_main, meterial_sub, meterial_color, origin_price, current_price)
		try:
			db.cursor.execute(sql)
			db.conn.commit()
		except Exception as e:
			print(e)

# insertData()


first = 1;
url = """http://m.asbox.cn/ajax_find_products.html?page=%s"""%(str(first))
data = requests.get(url).json()

print(data['data'])

resultList = data['data']['resultList']

print(resultList)

while len(resultList) > 0:
	for product in resultList:
		insertData(product['product_name'],product['product_url'],product['sku'],
					product['psku'],product['meterial_value'],product['meterial_value_2'],
					product['meterial_value_ys'],product['origin_price'],product['current_price'],)
		print(product)
		print('\n')
	first+=1
	url = 'http://m.asbox.cn/ajax_find_products.html?page=%s'%(str(first))
	resultList = requests.get(url).json()['data']['resultList']
	resultList = requests.get(url).json()['data']['resultList']


